﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECCSystem.EntityClass;
using ECCSystem.BizRule;

namespace ECCSystem.DataAccess
{
	class ClsKindOfTemplet : DBObject 
	{
		public DataSet GetKindOfTemplet()
		{
			DataSet objDataSet = new DataSet();
			objDataSet = RunProcedure("sp_KindOfTemplet_GetList", new IDataParameter[] { }, "tKindOfTemplet");

			return objDataSet;
			
		}
		public string AddKindOfTemplet(ArrayList objArr,SqlTransaction objTran)
		{
			try
			{

				SqlCommand objCmd = new SqlCommand("sp_KindOfTemplet_Add", BizRule.ClsCommon.gObjConn);
				int rowsAffected = 0;
				for (int i = 0; i < objArr.Count; i++)
				{
					KindOfTempletEntity objEntity = new KindOfTempletEntity();
					objEntity = (KindOfTempletEntity)objArr[i];

					SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier),
																	new SqlParameter("@ID", SqlDbType.NVarChar,1000),
																 new SqlParameter("@Name", SqlDbType.NVarChar, 50),
																 new SqlParameter("@ParentNode", SqlDbType.NVarChar,1000),
																 new SqlParameter("@Oreder", SqlDbType.BigInt),
																new SqlParameter("@UserID", SqlDbType.Int),
																new SqlParameter("@PersonalFlg", SqlDbType.BigInt)  };
					Guid guid = new Guid(objEntity.KeyID);
					parameters[0].Value = guid;
					parameters[1].Value = objEntity.ID;
					parameters[2].Value = objEntity.Name;
					parameters[3].Value = objEntity.ParentNode;
					parameters[4].Value = objEntity.Oreder;
					parameters[5].Value = ClsCommon.gIntOperatorID;
					if (ClsCommon.gStrRight == "CSR")
					{
						parameters[6].Value = 1;
					}
					else
					{
						parameters[6].Value = 0;
					}
					this.RunProcedure("sp_KindOfTemplet_Add", parameters, ref rowsAffected, objTran, objCmd);

					if (rowsAffected == 0)
					{
						objTran.Rollback();
						return "1";
					}
				}
				objTran.Commit();
				return "";
			}
			catch(Exception ex)
			{
				objTran.Rollback();
				return ex.ToString();
			}


		}
		public string DeleteKindOfTemplet(string lngID)
		{
			try
			{
				SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier) };
				Guid guid = new Guid(lngID);
				parameters[0].Value = guid;
				int rowsAffected = 0;
				this.RunProcedure("sp_KindOfTemplet_Delete", parameters, ref rowsAffected);
				return "";
			}
			catch(Exception ex)
			{
				return ex.ToString();
			}
		}

		public string DeleteKindOfTemplet(ArrayList strID, SqlTransaction objTran)
		{
			try
			{
				SqlCommand objCmd = new SqlCommand("sp_KindOfTemplet_Delete", BizRule.ClsCommon.gObjConn);
				for (int i = 0; i < strID.Count; i++)
				{
					
					SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier) };
					Guid guid = new Guid(strID[i].ToString());
					parameters[0].Value = guid;
					int rowsAffected = 0;
					this.RunProcedure("sp_KindOfTemplet_Delete", parameters, ref rowsAffected, objTran, objCmd);
					if (rowsAffected == 0)
					{
						objTran.Rollback();
						return "1";
					}
				}

				objTran.Commit();
				return "";
			}
			catch (Exception ex)
			{
				objTran.Rollback();
				return ex.ToString();
			}
		}

		public string UpdateKindOfTemplet(KindOfTempletEntity objEntity)
		{
			try
			{
				SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier),
																 new SqlParameter("@ID", SqlDbType.NVarChar, 1000),
																 new SqlParameter("@Name", SqlDbType.NVarChar, 50),
																 new SqlParameter("@ParentNode", SqlDbType.NVarChar, 1000),
																 new SqlParameter("@Oreder", SqlDbType.BigInt),
																new SqlParameter("@UserID", SqlDbType.Int),
																new SqlParameter("@PersonalFlg", SqlDbType.BigInt) };
				Guid guid = new Guid(objEntity.KeyID);
				parameters[0].Value = guid;
				parameters[1].Value = objEntity.ID;
				parameters[2].Value = objEntity.Name;
				parameters[3].Value = objEntity.ParentNode;
				parameters[4].Value = objEntity.Oreder;
				parameters[5].Value = ClsCommon.gIntOperatorID;
				if (ClsCommon.gStrRight == "CSR")
				{
					parameters[6].Value = 1;
				}
				else
				{
					parameters[6].Value = 0;
				}
				
				int rowsAffected = 0;

				this.RunProcedure("sp_KindOfTemplet_Update", parameters, ref rowsAffected);

				if (rowsAffected == 0)
				{
					return "1";
				}

				return "";
			}
			catch (Exception ex)
			{
				return ex.ToString();
			}


		}
	}
}
